import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const app=getApp();
let ServiceUrl = app.globalData.ServiceUrl;
Page({
  data: {
    value:'',
    user_id:'',
    active: '',
    role_state:'',
    listArr: [],//初始化列表数据
    //上拉加载
    page:1,
    isLastPage:false,
    tips:'上拉加载更多',
    empty: true
   },
  
  //搜索框值的回调
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  /**
   * [onSearch 按回车键进行搜索功能]
   * @return {[type]} [description]
   */
  onSearch() {
      //console.log(this.data.value);
      let that=this
      let user_id = wx.getStorageSync('uid')
      if(user_id){
        //向服务器传递数值
        wx.request({
          url: ServiceUrl + 'searchAssistance',
          data:{
            keyword:that.data.value,
            user_id:user_id
          },
          success:function(res){
            console.log(res);
            Toast('为您搜索到' + that.data.value + ' ' + res.data.length + '条记录');
            that.setData({
              listArr:res.data
            })
          },
          fail:function(res){
            wx.showModal({
              title:"提示",
              content:"发送请求失败",
              showCancel: false,
              confirmColor: 'skyblue',
            })
          }
        })
      }else{
        wx.showModal({
          cancelColor: 'cancelColor',
          content: '请先登录！',
          showCancel: false,
          confirmColor: 'skyblue',
          success (res){
            // if(res.confirm){
            //   wx.navigateBack({
            //     delta: 0,
            //   })
            //}
          }
        });
      }
  },
  /**
   * [onClick 点击搜索进行搜索功能]
   * @return {[type]} [description]
   */
  onClick() {
      //console.log(this.data.value);
      let that=this
      let user_id = wx.getStorageSync('uid')
      if(user_id){
        //向服务器传递数值
        wx.request({
          url: ServiceUrl + 'searchAssistance',
          data:{
            keyword:that.data.value,
            user_id:user_id
          },
          success:function(res){
            console.log(res);
            Toast('为您搜索到' + that.data.value + ' ' + res.data.length + '条记录');
            that.setData({
              listArr:res.data
            })
          },
          fail:function(res){
            wx.showModal({
              title:"提示",
              content:"发送请求失败",
              showCancel: false,
              confirmColor: 'skyblue',
            })
          }
        })
      }else{
        wx.showModal({
          cancelColor: 'cancelColor',
          content: '请先登录！',
          showCancel: false,
          confirmColor: 'skyblue',
          success (res){
            // if(res.confirm){
            //   wx.navigateBack({
            //     delta: 0,
            //   })
            //}
          }
        });
      }
    },
  /**
   * [onChangeTabs tabs选项卡]
   * @param  {[type]} event [description]
   * @return {[type]}       [description]
   */
  onChangeTabs(event){
    console.log(event)
    let index = event.detail.name;
    //重置分页数据，注意在页面有任何刷新的时候，都需要考虑分页数据的重置，当需要重置的次数很多，也可以把它放在一个函数中去调用
    this.setData({
      active:index,
      listArr:[],
      isLastPage:false,
      page:1
    });
    this.requestInfo(this.data.active);
  },
  /**
   * [onLoad 初始化本页面]
   * @param  {[type]} options [description]
   * @return {[type]}         [description]
   */
  onLoad:function (options) {
    console.log(options)
    this.setData({
      active:options.active,
      role_state:options.role_state,
      user_id:options.user_id
    });
    this.requestInfo(this.data.active)
  },
  /**
   * [onReachBottom 监听上拉动作函数]
   * @return {[type]} [description]
   */
  onReachBottom:function (){
    console.log('下拉了')
    if(this.data.isLastPage){
      return
    }
    this.setData({page:this.data.page + 1})
    this.requestInfo(this.data.active)
  },
  /**
   * [requestInfo 上拉加载列表数据函数]
   * @param  {[type]} active [description]
   * @return {[type]}        [description]
   */
  requestInfo(active){
    wx.showLoading({title:"加载中..."});
    setTimeout(res=>{
      wx.hideLoading();
    },1500)
    let that = this;
    let user_id = that.data.user_id;
    wx.request({
      url: ServiceUrl + 'selectAssistance',
      data:{
        user_id:user_id,
        active:that.data.active,
        page:that.data.page
      },
      success:(res)=>{
        console.log(res)
        if(res.data.state == 200){
          wx.hideLoading();
          let newData = {};
          if(res.data.data.length < 5){
            newData.isLastPage = true
            newData.tips = "已显示全部了"
          }
            newData.empty = false
          //追加数据
          newData.listArr = that.data.listArr.concat(res.data.data)
          that.setData(newData)
        }else if(res.data.state == 401 && that.data.page == 1){
          wx.hideLoading();
          that.setData({
            empty: true
          })
        }
      },
      fail:function(res){
        wx.showModal({
          title:"提示",
          content:"发送请求失败",
          showCancel: false,
          confirmColor: 'skyblue',
        })
      }
    })
  },
  /**
   * [onPullDownRefresh 监听下拉刷新函数]
   * @return {[type]} [description]
   */
  onPullDownRefresh:function(){
    //console.log(this.data.active)
    //重置分页数据
    this.setData({
      active:this.data.active,
      listArr:[],
      isLastPage:false,
      page:1
    });
    this.requestInfo(this.data.active);//重新加载列表数据
    wx.stopPullDownRefresh();//刷新完成后停止下拉刷新动效
  },
  onShow: function(){
  },
  onReady: function(){
  },
  onHide: function(){
  },
  onUnload: function(){
  }
})